//
//  UIWebViewDemoController.m
//  advertiseDemo
//
//  Created by zhangliang on 16/11/29.
//  Copyright © 2016年 zhouhuanqiang. All rights reserved.
//

#import "UIWebViewDemoController.h"

@interface UIWebViewDemoController ()

@property (nonatomic, strong) UIWebView *webView;

@end

@implementation UIWebViewDemoController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"加载webView";
    _webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    _webView.backgroundColor = [UIColor whiteColor];
    if (!self.adUrl) {
        self.adUrl = [[NSBundle mainBundle] pathForResource:@"javascript_UIweb" ofType:@"html"];
    }
    NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.adUrl]];
    [_webView loadRequest:request];
    [self.view addSubview:_webView];
    
    JSContext *_context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    external *nal = [external new];
    _context[@"external"] = nal;
}

// 测试
- (void)loadWebText {
    NSString *html = @"<!DOCTYPE html><html><head><meta charset=\"utf-8\"><title>test javascript</title></head><body><div><!--为什么使用ttf.调用，下面会讲到--><button onclick=\"external.supercall({cmd:1,data:{a:1}});\">点击我然后看xcode的log</button></div></body></html>";
    [_webView loadHTMLString:html baseURL:nil];
}

- (void)setAdUrl:(NSString *)adUrl
{
    _adUrl = adUrl;
}

@end
